// pages/practiceJoin/index.js
let app = getApp()
let handleStart = false
Page({
  /**
   * 页面的初始数据
   */
  data: {
    startDate: '',
    trainingId: '',
    trainingName: '',
    detailId: '',
    questionnaireId: '',
    trainingType: '',
    activityDialogShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {
      startDate,
      trainingId,
      trainingName,
      detailId,
      questionnaireId,
      trainingType,
      isNeedEject,
    } = options

    let date = startDate.split(' ')[0]

    this.setData({
      startDate: date,
      trainingId: trainingId,
      trainingName: trainingName,
      detailId: detailId,
      questionnaireId: questionnaireId,
      trainingType: trainingType,
      activityDialogShow: Number(isNeedEject) === 1,
    })
  },
  // 开始第一次训练
  handleBack() {
    let { detailId, trainingType, questionnaireId, trainingId } = this.data
    app.globalData.finishTaskEntrance = 3
    app.globalData.isFirstTask = true
    handleStart = true
    if (Number(trainingType) === 1) {
      wx.redirectTo({
        url: '/pages/sceneTaskDetail/index?id=' + detailId,
      })
    } else {
      wx.redirectTo({
        url:
          '/pages/selfAssessment/question/index?key=1&id=' +
          questionnaireId +
          '&pathId=' +
          trainingId +
          '&taskFlowId=' +
          detailId,
      })
    }
  },
  // 关闭活动弹窗
  handleCloseActivity() {
    this.setData({
      activityDialogShow: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    handleStart = false
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (handleStart) {
      return
    }
    let createPatientEntrance = app.globalData.createPatientEntrance
    if (createPatientEntrance === 5) {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
